package edu.spec.teacher.dao.impl;

import edu.spec.sys.model.enumeration.SignInStatus;
import edu.spec.sys.model.enumeration.Status;
import edu.spec.teacher.dao.iface.IUserSignInDao;
import edu.spec.teacher.model.SignIn;
import edu.spec.teacher.model.UserSignIn;
import org.madgecko.basic.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Author       : MadFrog
 * Date         : 2018/10/9 19:33
 * Description  :
 */
@Repository
public class UserSignInDao extends BaseDao<UserSignIn> implements IUserSignInDao {
    @Override
    public List<SignIn> listEffectiveSignInByUser(Integer userId) {
        String hql = "select us.signIn from UserSignIn us where us.user.id=" + userId + " and us.signIn.status=" + Status.ENABLED.ordinal() + " and us.verdict=" + SignInStatus.UNSIGNIN.ordinal();
        return this.list(hql);
    }

    @Override
    public UserSignIn loadByUserAndSignIn(Integer userId, Integer signInId) {
        long current = System.currentTimeMillis();
        // 签到开始3小时候后，签到记录不可见
        current += 3 * 60 * 60 * 1000;
        Date latestDate = new Date(current);

        String hql = " from UserSignIn usi where usi.user.id=" + userId + " and usi.signIn.id=" + signInId;
        return (UserSignIn)this.queryObject(hql);
    }
}
